﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Windows.Forms;
using DataAccessLayer;
using BusinessAccessLayer;
using DevExpress.XtraReports.UI;
using DevExpress.XtraEditors.Controls;
using System.IO;
using DevExpress.XtraEditors.Repository;

namespace QuanLyBanHang
{
    public partial class FormMain : XtraForm
    {

        
        BAThanhPho tp = new BAThanhPho();
        BAKhachHang kh = new BAKhachHang();
        BASanPham sp = new BASanPham();
        BAHoaDon hd = new BAHoaDon();
        BAChiTietHoaDon cthd = new BAChiTietHoaDon();
        BANhanVien nv = new BANhanVien();

        public FormMain()
        {
            InitializeComponent();

        }

        private void barButtonItemXemSP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.DataSource = sp.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemXemDMThanhPho_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            gridControl.DataSource = tp.getTable();
            gridView1.PopulateColumns();

        }

        private void barButtonItemThemSP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemSP themsp = new FormThemSP();
            themsp.ShowDialog();
            gridControl.DataSource = sp.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemThemTP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemThanhPho themTP = new FormThemThanhPho();
            DialogResult kq = themTP.ShowDialog();
            this.Show();
            gridControl.DataSource = tp.getTable();
            gridView1.PopulateColumns();

        }

        private void barButtonItemSuaTP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView rowtp = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string matp = rowtp[0].ToString();
            string tentp = rowtp[1].ToString();
            FormThemThanhPho themtp = new FormThemThanhPho();
            themtp.labelControl1.Text = "SỬA THÀNH PHỐ";
            themtp.txtMaTP.Text = matp;
            themtp.txtTenTP.Text = tentp;
            themtp.simpleButtonThemTP.Text = "Sửa TP";
            DialogResult kq = themtp.ShowDialog();
            //themtp.Show();
            gridControl.DataSource = tp.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemXoaTP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView tableTP = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string matp = tableTP["MaTP"].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa thành phố không?"),
                   "Xóa Công Việc",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    tp.XoaThanhPho(matp);
                    MessageBox.Show("Xóa thành phố thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    gridControl.DataSource = tp.getTable();
                    gridView1.PopulateColumns();

                }
                catch
                {
                    MessageBox.Show("Xóa thành phố thất bại !!", "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void barButtonItemXemDMKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.DataSource = kh.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemThemKH_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemKH themkh = new FormThemKH();
            themkh.ShowDialog();
            gridControl.DataSource = kh.getTable();
            gridView1.PopulateColumns();


        }

        private void barButtonItemSuaKH_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView kh_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string makh = kh_row[0].ToString();
            string tencty = kh_row[1].ToString();
            string diachi = kh_row[2].ToString();
            string thanhpho = kh_row[3].ToString();
            string dienthoai = kh_row[4].ToString();
            FormThemKH frmthemkh = new FormThemKH();
            frmthemkh.labelControl1.Text = "SỬA KHÁCH HÀNG";
            frmthemkh.simpleButtonThemKH.Text = "Sửa Khách Hàng";
            frmthemkh.txtMaKH.Text = makh;
            frmthemkh.txtTenCty.Text = tencty;
            frmthemkh.txtDiaChi.Text = diachi;
            frmthemkh.txtTP.Text = thanhpho;
            frmthemkh.txtDienThoai.Text = dienthoai;
            frmthemkh.ShowDialog();
            gridControl.DataSource = kh.getTable();
            gridControl.RefreshDataSource();
            gridView1.PopulateColumns();
        }

        private void barButtonItemXoaKH_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView kh_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string makh = kh_row[0].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa khách hàng {0} không?", kh_row[2].ToString()),
                   "Xóa Khách Hàng  ",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    kh.XoaKhachHang(makh);
                    MessageBox.Show("Xóa khách hàng thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    gridControl.DataSource =
                        kh.getTable();
                    gridView1.PopulateColumns();

                }
                catch
                {
                    MessageBox.Show("Xóa khách hàng thất bại !!",
                       "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void barButtonItem15_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.DataSource = nv.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemXemHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.DataSource = hd.getTable();
            gridView1.PopulateColumns();

        }

        private void barButtonItemThemHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemHoaDon themhd = new FormThemHoaDon();
            themhd.ShowDialog();

            gridControl.DataSource = hd.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemSuaHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView hd_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string MaHD = hd_row[0].ToString();
            string MaKH = hd_row[1].ToString();
            string MaNV = hd_row[2].ToString();
            DateTime NgayLapHD = DateTime.Parse(hd_row[3].ToString());
            string NgayNhanHang = hd_row[4].ToString(); ;
            FormThemHoaDon themhd = new FormThemHoaDon();
            themhd.txtMaHD.Text = MaHD;
            themhd.txtMaKH.Text = MaKH;
            themhd.txtMaNV.Text = MaNV;
            themhd.date_NgaylapHD.EditValue = NgayLapHD;
            themhd.dateNgayNhanHang.EditValue = NgayNhanHang;
            themhd.labelControl1.Text = "SỬA HÓA ĐƠN";
            themhd.btnThemHD.Text = "Sửa Hóa Đơn";
            themhd.ShowDialog();
            gridControl.DataSource = hd.getTable();
            gridControl.RefreshDataSource();
            gridView1.PopulateColumns();

        }

        private void barButtonItemXoaHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView hd_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string mahd = hd_row[0].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa hóa đơn {0} không?", hd_row[0].ToString()),
                   "Xóa Hóa Đơn",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    hd.XoaHoaDon(mahd);
                    MessageBox.Show("Xóa hóa đơn thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    gridControl.DataSource =
                        hd.getTable();
                    gridView1.PopulateColumns();

                }
                catch
                {
                    MessageBox.Show("Xóa hóa đơn thất bại !!",
                       "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void barButtonItemXemCTHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.DataSource = cthd.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemThemCTHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemCTHD themcthd = new FormThemCTHD();
            themcthd.ShowDialog();
            cthd = new BAChiTietHoaDon();
            gridControl.DataSource = cthd.getTable();
            gridView1.PopulateColumns();
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FormIn fi = new FormIn();
            fi.Show();
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FormInTable table = new FormInTable();
            table.Show();
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FormInDrillThrought drillthrought = new FormInDrillThrought();
            drillthrought.Show();
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FormInChart ch = new FormInChart();
            ch.Show();
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView sp_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string masp = sp_row[0].ToString();
            string tensp = sp_row[1].ToString();
            string dvtinh = sp_row[2].ToString();
            string dongia = sp_row[3].ToString();
            string maloaisp = sp_row[4].ToString();

            FormThemSP themsp = new FormThemSP();
            themsp.txtMaSP.Text = masp;
            themsp.txtTenSP.Text = tensp;
            themsp.txtDonViTinh.Text = dvtinh;
            themsp.txtDongia.Text = dongia;
            themsp.txtMaloaiSP.Text = maloaisp;
            themsp.labelControl6.Text = "SỬA SẢN PHẨM";
            themsp.simpleButtonThemSP.Text = "Sửa Sản Phẩm";
            themsp.ShowDialog();
            BASanPham spp = new BASanPham();
            gridControl.DataSource = spp.getTable();
            gridView1.PopulateColumns();


        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView sp_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string masp = sp_row[0].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa sản phẩm {0} không?", sp_row[0].ToString()),
                   "Xóa Sản Phẩm",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    sp.XoaSanPham(masp);
                    MessageBox.Show("Xóa sản phẩm thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    BASanPham spp = new BASanPham();
                    gridControl.DataSource = spp.getTable();
                    gridView1.PopulateColumns();


                }
                catch
                {
                    MessageBox.Show("Xóa sản phẩm thất bại !!",
                       "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void barButtonItem16_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormThemNhanVien themnv = new FormThemNhanVien();
            themnv.ShowDialog();
            gridControl.DataSource = nv.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItem17_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView nv_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string manv = nv_row[0].ToString();
            string Ho = nv_row[1].ToString();
            string Ten = nv_row[2].ToString();
            bool GT = (bool)nv_row[3];
            DateTime NgayNV = DateTime.Parse(nv_row[4].ToString());
            string Diachi = nv_row[5].ToString(); ;
            string DienThoai = nv_row[6].ToString();

            FormThemNhanVien frmthemnv = new FormThemNhanVien();
            frmthemnv.labelControl1.Text = "SỬA NHÂN VIÊN";
            frmthemnv.txtMaNV.Text = manv;
            frmthemnv.txtHo.Text = Ho;
            frmthemnv.txtTen.Text = Ten;
            frmthemnv.dateNgaynhanvien.EditValue = NgayNV.ToString();
            frmthemnv.chbGioitinh.Checked = GT;
            frmthemnv.txtDiachi.Text = Diachi;
            frmthemnv.txtDienthoai.Text = DienThoai;
            frmthemnv.simpleButtonThem.Text = "Sửa Nhân Viên";
            frmthemnv.ShowDialog();
            BANhanVien xx = new BANhanVien();
            gridControl.DataSource = xx.getTable();
            gridView1.PopulateColumns();


        }

        private void barButtonItem18_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView nv_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string manv = nv_row[0].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa nhân viên {0} không?", nv_row[0].ToString()),
                   "Xóa Nhân Viên",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    nv.XoaNhanVien(manv);
                    MessageBox.Show("Xóa nhân viên thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    gridControl.DataSource = nv.getTable();
                    gridView1.PopulateColumns();


                }
                catch
                {
                    MessageBox.Show("Xóa nhân viên thất bại !!",
                       "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void barButtonItemSuaCTHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView cthd_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
           // string MaHD = cthd_row[0].ToString();
           // string MaSP = cthd_row[1].ToString();
            string SoLuong = cthd_row[0].ToString();
            FormThemCTHD themcthd = new FormThemCTHD();
            
            themcthd.txtSoLuong.Text = SoLuong;
            themcthd.labelControl1.Text = " CẬP NHẬT CTHD";
            themcthd.simpleButtonThemCTHD.Text = "Cập nhật CTHĐ";
            themcthd.ShowDialog();

            BAChiTietHoaDon ct = new BAChiTietHoaDon();
            gridControl.DataSource = ct.getTable();
            gridView1.PopulateColumns();
        }

        private void barButtonItemXoaCTHD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRowView cthd_row = (DataRowView)gridView1.Columns[0].View.GetFocusedRow();
            string mahd = cthd_row[0].ToString();
            string masp = cthd_row[1].ToString();
            DialogResult tl = MessageBox.Show(String.Format("Có xóa CTHD có MaHD {0} và MaSP {1} không?", cthd_row[0].ToString(), cthd_row[1].ToString()),
                   "Xóa CTHD",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (tl == DialogResult.Yes)
            {
                try
                {
                    cthd.XoaCTHD(mahd, masp);
                    MessageBox.Show("Xóa CTHD thành công!!",
                       "Kết quả",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                    gridControl.DataSource =
                        cthd.getTable();
                    gridView1.PopulateColumns();

                }
                catch
                {
                    MessageBox.Show("Xóa CTHD thất bại !!",
                       "Lỗi",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }
    }
}
